package cn.lee.cplibrary.widget.recycler.decoration;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;


/**
 * RecyclerView网格布局实现间距（GridLayoutManager）
 * 不可以与Adapter的头布局/尾布局一起使用，会错乱
 * 不可以与StaggeredGridLayoutManager一起使用，会错乱
 * Create by ChrisLee on 2021-09-14
 */
public class GridSpacingDecoration extends RecyclerView.ItemDecoration {

    private int marginCol; //第1列距左边、最后一列距右边的距离
    private int marginRaw; //第1行距上边、最后一行距下边的距离
    private int itemSpacingCol; //item的列间隔（去掉首尾列）
    private int itemSpacingRaw; //item的行间隔（去掉首尾行）


    public GridSpacingDecoration(int marginCol, int itemSpacing) {
        this.marginCol = marginCol;
        this.itemSpacingCol = itemSpacing;
    }

    public GridSpacingDecoration(int marginCol, int marginRaw, int itemSpacingCol, int itemSpacingRaw) {
        this.marginCol = marginCol;
        this.marginRaw = marginRaw;
        this.itemSpacingCol = itemSpacingCol;
        this.itemSpacingRaw = itemSpacingRaw;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int spanCount = getSpanCount(parent);
        int position = parent.getChildAdapterPosition(view); //item position
        //int column = position % spanCount; // item column
        //列间距
        if (isFirstColumn(parent, position, spanCount)) {
            outRect.left = marginCol;
            outRect.right = itemSpacingCol / 2;
        } else if (isLastColumn(parent, position, spanCount)) {
            outRect.left = itemSpacingCol / 2;
            outRect.right = marginCol;
        } else {
            outRect.left = itemSpacingCol / 2;
            outRect.right = itemSpacingCol / 2;
        }
        //行间距
        if (isFirstRow(parent, position,spanCount, getChildCount(parent))) {
            outRect.top = marginRaw;
            outRect.bottom = itemSpacingRaw / 2;
        } else if (isLastRow(parent, position, spanCount, getChildCount(parent))) {
            outRect.top = itemSpacingRaw / 2;
            outRect.bottom = marginRaw;
        } else {
            outRect.top = itemSpacingRaw / 2;
            outRect.bottom = itemSpacingRaw / 2;
        }
    }

    /**
     * 判读是否是第一列
     *
     * @param parent
     * @param pos
     * @param spanCount
     * @return spanCount
     */
    private boolean isFirstColumn(RecyclerView parent, int pos, int spanCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            if (pos % spanCount == 0) {
                return true;
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager)
                    .getOrientation();
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                if (pos % spanCount == 0) {// 第一列
                    return true;
                }
            } else {

            }
        }
        return false;
    }

    /**
     * 判断是否是最后一列
     *
     * @param parent
     * @param pos
     * @param spanCount
     * @param spanCount
     * @return
     */
    private boolean isLastColumn(RecyclerView parent, int pos, int spanCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            if ((pos + 1) % spanCount == 0) {// 如果是最后一列，则不需要绘制右边
                return true;
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager)
                    .getOrientation();
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                if ((pos + 1) % spanCount == 0) {// 最后一列
                    return true;
                }
            } else {

            }
        }
        return false;
    }

    /**
     * 判读是否是最后一行
     *
     * @param parent
     * @param pos
     * @param spanCount
     * @param childCount
     * @return
     */
    private boolean isLastRow(RecyclerView parent, int pos, int spanCount, int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            int lines = childCount % spanCount == 0 ? childCount / spanCount : childCount / spanCount + 1;
            return lines == pos / spanCount + 1;
        }
        return false;
    }

    /**
     * 判断是否是第一行
     *
     * @param parent
     * @param pos
     * @param spanCount
     * @param childCount
     * @return
     */
    private boolean isFirstRow(RecyclerView parent, int pos, int spanCount, int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            if ((pos / spanCount + 1) == 1) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * 获取列数
     *
     * @param parent
     * @return
     */
    private int getSpanCount(RecyclerView parent) {
        int spanCount = -1;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
        }
        return spanCount;
    }
    /**
     * 获取总Item数量
     *
     * @param parent
     * @return
     */
    private int getChildCount(RecyclerView parent) {
        int childCount = parent.getAdapter().getItemCount();
        return childCount;
    }

    public void setMarginCol(int marginCol) {
        this.marginCol = marginCol;
    }

    public void setMarginRaw(int marginRaw) {
        this.marginRaw = marginRaw;
    }

    public void setItemSpacingCol(int itemSpacingCol) {
        this.itemSpacingCol = itemSpacingCol;
    }

    public void setItemSpacingRaw(int itemSpacingRaw) {
        this.itemSpacingRaw = itemSpacingRaw;
    }
}
